package com.seata.logic.visualize.tcc.impl;

import com.seata.core.utils.IdempotentUtils;
import com.seata.logic.visualize.dao.TblStorageMapper;
import com.seata.logic.visualize.entity.TblStorage;
import com.seata.logic.visualize.tcc.StorageTccAction;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author 蚂蚁会花呗
 * @date 2021/9/27 10:31
 */
@Service
public class StorageTccActionImpl implements StorageTccAction {

    static Log log = LogFactory.getLog(StorageTccActionImpl.class);

    @Autowired
    private TblStorageMapper tblStorageMapper;

    @Override
    @Transactional
    public boolean decreaseStorage(BusinessActionContext businessActionContext, String prodId, Integer count) {
        if (Objects.nonNull(IdempotentUtils.getMarker(getClass(),businessActionContext.getXid()))){
            log.info("已执行过try阶段");
            return true;
        }
        //首先冻结
        TblStorage tblStorage = tblStorageMapper.selectById(prodId);
        tblStorage.setTotal(tblStorage.getTotal() - count);
        tblStorage.setFrozen(tblStorage.getFrozen() + count);
        int result = tblStorageMapper.updateById(tblStorage);
        IdempotentUtils.addMarker(getClass(),businessActionContext.getXid(),"marker");
        return result > 0;
    }

    @Override
    @Transactional
    public boolean commit(BusinessActionContext businessActionContext) {
        if (Objects.isNull(IdempotentUtils.getMarker(getClass(),businessActionContext.getXid()))){
            log.info("已执行过commit阶段");
            return true;
        }
        //产品id
        String prodId = businessActionContext.getActionContext("prodId").toString();
        //订单数量
        Integer count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
        TblStorage tblStorage = tblStorageMapper.selectById(prodId);
        //设置已使用库存
        tblStorage.setUsed(tblStorage.getUsed() + count);
        //设置冻结
        tblStorage.setFrozen(tblStorage.getFrozen() - count);
        int result = tblStorageMapper.updateById(tblStorage);
        IdempotentUtils.removeMarker(getClass(),businessActionContext.getXid());
        return result > 0;
    }

    @Override
    @Transactional
    public boolean rollback(BusinessActionContext businessActionContext) {
        if (Objects.isNull(IdempotentUtils.getMarker(getClass(),businessActionContext.getXid()))){
            log.info("已执行过rollback阶段");
            return true;
        }
        //产品id
        String prodId = businessActionContext.getActionContext("prodId").toString();
        //订单数量
        Integer count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
        TblStorage tblStorage = tblStorageMapper.selectById(prodId);

        tblStorage.setTotal(tblStorage.getTotal() + count);
        tblStorage.setFrozen(tblStorage.getFrozen() - count);
        int result = tblStorageMapper.updateById(tblStorage);
        IdempotentUtils.removeMarker(getClass(),businessActionContext.getXid());
        return result > 0;
    }
}
